function [ids] =  getStimIDs(aux_data, varargin)

aux_ch_frame_times = 2;
aux_ch_stim = 4;
ids = struct;
frame_cutoff = 0;

if ~isempty(varargin)
    numIndex = find(cellfun('isclass', varargin(1:end-1), 'char'));
    for ind = 1:length(numIndex)
        switch lower(varargin{numIndex(ind)})
            case 'stimfile'
                stimfile = varargin{numIndex(ind) + 1};
            case 'aux_ch_frame_times'
                aux_ch_frame_times = varargin{numIndex(ind) + 1};
            case 'aux_ch_stim'
                aux_ch_stim = varargin{numIndex(ind) + 1};
            case 'frame_cutoff'
                frame_cutoff = varargin{numIndex(ind) + 1};
        end
    end
end


% aux_data = load_lvd(['D:\data\mouse\sparse\' animal '\Data\' expdate filesep auxID]);
stiminfo =  readini(stimfile);
frame_times = get_frame_times(aux_data(aux_ch_frame_times,:));

% var_level = 0.1;
% try to get the stim conditions from the analog signal
ids.stim_frames = aux_data(aux_ch_stim, frame_times) - min(aux_data(aux_ch_stim, frame_times));
% stim_conditions = unique(fix(stim_frames * 10)) / 10;
% stim_conditions = stim_conditions(stim_conditions > 0.75); % remove 0 and optional offset at the beginning
% stim_on = uint8(zeros(1, length(stim_frames)));
% for ind = 1:length(stim_conditions)
%     stim_on(find((stim_frames > stim_conditions(ind) - var_level) & (stim_frames < stim_conditions(ind) + var_level))) = ind;
% end
ids.stim_off = ids.stim_frames < 0.1;
ids.stim_on_bin = ids.stim_frames > 0.75;
c = sort([0 strfind(ids.stim_on_bin,[1,0]) length(ids.stim_on_bin)]);
c = c(1:(stiminfo.orientations * stiminfo.repetitions)+1);
stimseq = (stiminfo.seq / (360/length(unique(stiminfo.seq)))) + 1;
ids.stimseq_deg = stiminfo.seq;
ids.stim_on = [];
for ii=1:length(c)-1,
    ids.stim_on = [ids.stim_on ids.stim_on_bin(c(ii)+1:c(ii+1))*stimseq(ii)];
end
ids.stim_on_deg = [];
for ii=1:length(c)-1,
    temp = int16(ids.stim_on_bin(c(ii)+1:c(ii+1)));
    temp(temp == 0) = -1;
    temp(temp == 1) = ids.stimseq_deg(ii);
    ids.stim_on_deg = [ids.stim_on_deg temp];
end

ids.stim_on = [ids.stim_on zeros(1,length(ids.stim_on_bin) - length(ids.stim_on))];
ids.stim_on_deg = [ids.stim_on_deg ones(1,length(ids.stim_on_bin) - length(ids.stim_on_deg)) * -1];
stim_repetitions = sum(diff(ids.stim_on) == 1);

c = sort([1 strfind(ids.stim_off,[1,0])+1 strfind(ids.stim_on_bin,[1,0])+1 length(ids.stim_on_bin)]);
if (length(c) < (stiminfo.orientations * stiminfo.repetitions * 2)+1)
    c = [c zeros(1,stiminfo.orientations * stiminfo.repetitions * 2+1 - length(c))* NaN];
    % puts NaN onto positions which are not valid, e.g. stim not done when
    % image aquisition was finished
end
        
c = c(1:(stiminfo.orientations * stiminfo.repetitions * 2)+1);
e = [];
e(2:3:stiminfo.orientations * stiminfo.repetitions * 3) = c(2:2:end);
e(3:3:end+1) = c(3:2:end) - 1;
e(1:3:end) = c(1:2:end-1);
e(1) = max(find((ids.stim_on_bin+ids.stim_off) == 0)) + 1;
ids.stim_boundaries = reshape(e, 3, length(e)/3)';


% let's reshuffle the stim to bring in order
[~,f] = sort(reshape(stimseq, max(stimseq), stim_repetitions)',2); %#ok<UDIM>
f = f + repmat(0:max(stimseq):max(stimseq)*(stim_repetitions - 1), max(stimseq), 1)';
ids.stim_boundaries = ids.stim_boundaries(reshape(f',1,numel(f)),:); % reshuffle
ids.stim_boundaries = reshape(ids.stim_boundaries, max(stimseq), stim_repetitions, 3);
% stim_boundaries(stim category, repetitions, [stim_off_start stim_on_start stim_on_end])

ids.stim_off_cond = zeros(size(ids.stim_off));
ids.stim_on_bin_w_cutoff = logical(zeros(size(ids.stim_on)));

for ind = 1:max(stimseq)
    
    for knd = 1:stim_repetitions
        ids.stim_off_cond(ids.stim_boundaries(ind,knd,1):ids.stim_boundaries(ind,knd,2)-1) = ind;
        ids.stim_on_bin_w_cutoff(ids.stim_boundaries(ind,knd,2) + frame_cutoff:ids.stim_boundaries(ind,knd,3)) = 1;
    end
    
end


